home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / AExamples / MemorySrc.a < prev    next >
Encoding:
Text File  |  1998-12-03  |  7.9 KB  |  277 lines  |  [TEXT/MPS ]

  1. ***************************************************************************
  2. ****    
  3. ****    MEMORY DESK ACCESSORY - A sample DA written in MPW 68000 Assembly
  4. ****    
  5. ****    Copyright Apple Computer, Inc. 1985-1987, 1993, 1998
  6. ****    All rights reserved.
  7. ****
  8. ***************************************************************************
  9.  
  10.         STRING    PASCAL
  11.  
  12.         MAIN
  13.  
  14. OLDROUTINENAMES    Set    1    ; allow old LowMem identifiers
  15.  
  16.         INCLUDE    'Events.a'    
  17.         INCLUDE 'Devices.a'
  18.         INCLUDE 'Files.a'
  19.         INCLUDE 'Fonts.a'
  20.         INCLUDE 'LowMemEqu.a'
  21.         INCLUDE 'Memory.a'
  22.         INCLUDE    'Traps.a'
  23.         INCLUDE 'Quickdraw.a'
  24.         INCLUDE    'ToolUtils.a'
  25.         INCLUDE    'Windows.a'
  26.         INCLUDE 'NumberFormatting.a'
  27.  
  28. ; Desk accessories (drivers) cannot use global variables in the normal sense.
  29. ; Usually, a handle is allocated and stuffed into dCtlStorage and global
  30. ; variables are stored in this handle.  However, in this example, the globals
  31. ; are allocated at the end of the desk accessory's window record.  Since the
  32. ; window record is always nonrelocatable storage, the variables will never move.
  33. ; This record structure below defines the layout of our "global variables."
  34.  
  35. GlobalVars    RECORD    WindowRecord.sizeof        ; Put variables at end of window rec
  36. aString     DS.B    28                        ; vol names must be < 28 char
  37. aNumStr     DS.B    10                        ; sufficient for 10 GB of space
  38. GlobalSize    EQU        *-GlobalVars            ; size of my globals
  39.             ENDR
  40.  
  41.     WITH GlobalVars
  42.  
  43. aPBPtr    EQU     D7
  44.  
  45.  
  46. **************************** DESK ACCESSORY ENTRY **************************
  47.         
  48. DAEntry                                         ; See Device Manager IM:2
  49.     DC.B    (1<<dCtlEnable) + (1<<dNeedTime)    ; periodic,control flags set
  50.     DC.B    0                                    ; Lower byte is unused
  51.     DC.W    5*60                                ; 5 sec periodic update
  52.     DC.W    (1<<updateEvt)                        ; Handle only update events
  53.     DC.W    0                                    ; No menu for this accessory
  54.  
  55.     DC.W    DAOpen-DAEntry                        ; Open routine
  56.     DC.W    DADone-DAEntry                        ; Prime - unused
  57.     DC.W    DACtl-DAEntry                        ; Control
  58.     DC.W    DADone-DAEntry                        ; Status - unused
  59.     DC.W    DAClose-DAEntry                     ; Close
  60.  
  61. DATitle
  62.     DC.B    'Free Mem (#Bytes)'                 ; DA Name (& Window Title)
  63.     ALIGN    2                                    ; Word align
  64.  
  65.  
  66. ************************ DESK ACCESSORY OPEN ROUTINE ***********************
  67.  
  68. DAOpen
  69.     MOVEM.L     A1-A4,-(SP)                     ; preserve A1-A4
  70.     MOVE.L        A1,A4                            ; MOVE DCE pointer to a reg
  71.     
  72.     SUBQ.L        #4,SP                            ; FUNCTION = GrafPtr
  73.     MOVE.L        SP,-(SP)                        ; push a pointer to it
  74.     _GetPort                                    ; push it on top of stack
  75.     TST.L        DCtlEntry.dCtlWindow(A4)        ; do we have a window?
  76.     BNE.S        StdReturn                        ; If so, return, Else…
  77.  
  78. ******************************* NEW WINDOW ROUTINE *************************
  79.     WITH        WindowRecord
  80.     
  81.     MOVE.L        #sizeof+GlobalSize,D0
  82.     _NewPtr                                        ; allocate space for record
  83.     SUBQ        #4,SP                            ; FUNCTION = WindowRef
  84.     MOVE.L        A0,-(SP)                        ; address of storage
  85.     PEA         theWindow                        ; boundsRect
  86.     PEA         DATitle                         ; title
  87.     CLR.W        -(SP)                            ; visible flag FALSE
  88.     MOVE.W        #noGrowDocProc,-(SP)            ; window proc
  89.     MOVE.L        #-1,-(SP)                        ; window in front
  90.     MOVE.B        #1,-(SP)                        ; goAway box TRUE
  91.     CLR.L        -(SP)                            ; refCon is 0
  92.     _NewWindow
  93.     MOVE.L        (SP)+,A0
  94.     MOVE.L        A0,DCtlEntry.DCtlWindow(A4)                ; save windowPtr
  95.     MOVE.W        DCtlEntry.DCtlRefNum(A4),windowKind(A0)    ; system window
  96.     _MaxMem
  97.     
  98. StdReturn
  99.     _SetPort                                    ; old port on stack
  100.     MOVEM.L     (SP)+,A1-A4                     ; restore regs
  101.     ENDWITH
  102.  
  103.  
  104. ************************ DESK ACCESSORY DONE ROUTINE ***********************
  105.  
  106. DADone
  107.     MOVEQ        #0,D0                            ; return no error
  108.     RTS                                         ; all done, exit
  109.  
  110.  
  111. ************************ DESK ACCESSORY CLOSE ROUTINE **********************
  112.  
  113. DAClose
  114.     MOVEM.L     A1-A4,-(SP)                     ; preserve A1-A4
  115.     MOVE.L        A1,A4                            ; MOVE DCE ptr to A4
  116.  
  117.     SUBQ.L        #4,SP                            ; FUNCTION = GrafPtr
  118.     MOVE.L        SP,-(SP)                        ; push a pointer to it
  119.     _GetPort                                    ; get it, now it's on TOS
  120.  
  121.     MOVE.L        DCtlEntry.DCtlWindow(A4),-(SP)    ; push the window
  122.     _DisposeWindow                                ; dispose of the window
  123.  
  124.     CLR.L        DCtlEntry.DCtlWindow(A4)        ; mark DCE properly
  125.     BRA.S        StdReturn                        ; all done with close, exit
  126.  
  127.  
  128. ********************** DESK ACCESSORY CONTROL ROUTINE **********************
  129.  
  130. DACtl
  131.     MOVE.L        A4,-(SP)                        ; preserve reg
  132.     MOVE.L        A1,A4                            ; move DCE ptr to A4
  133.     MOVE.W        CntrlParam.CSCode(A0),D0        ; get the control opCode
  134.     SUB.W        #accEvent,D0                    ; = 64? (event)
  135.     BEQ.S        DoCtlEvent
  136.     SUB.W        #1,D0                            ; = 65? (periodic)
  137.     BEQ.S        DoPeriodic
  138.  
  139. CtlDone
  140.     MOVE.L        A4,A1                            ; put DCE ptr back in A1
  141.     MOVE.L        (SP)+,A4                        ; restore reg
  142.     MOVEQ        #0,D0                            ; return no error
  143.     MOVE.L        JIODone,-(SP)                    ; jump to IODone
  144.     RTS
  145.  
  146.  
  147. ************************** EVENT HANDLING ROUTINE **************************
  148.  
  149. DoCtlEvent
  150.     MOVE.L        A3,-(SP)                        ; save reg
  151.     MOVE.L        CntrlParam.CSParam(A0),A3        ; get the event pointer
  152.     MOVE.W        EventRecord.what(A3),D0            ; get the event number
  153.     SUBQ        #updateEvt,D0                    ; is it an update?
  154.     BNE.S        CtlEvtDone                        ; If not, exit
  155.  
  156.     MOVE.L        EventRecord.message(A3),-(SP)    ; push windowPtr
  157.     _BeginUpdate                                ; begin the update operation
  158.     
  159.     MOVE.L        EventRecord.message(A3),-(SP)    ; push windowPtr again
  160.     _SetPort
  161.     BSR.S        DrawWindow                        ; draw our items
  162.         
  163.     MOVE.L        EventRecord.message(A3),-(SP)    ; one more time
  164.     _EndUpdate                                    ; end of update
  165.  
  166. CtlEvtDone
  167.     MOVE.L        (SP)+,A3                        ; restore reg
  168.     BRA.S        CtlDone                         ; exit
  169.  
  170.  
  171. **************************** PERIODIC ROUTINE *****************************
  172.  
  173. DoPeriodic
  174.     MOVE.L        DCtlEntry.DCtlWindow(A4),-(SP)    ; set the port
  175.     _SetPort
  176.  
  177.     BSR.S        DrawWindow                        ; draw our window every 5s
  178.     BRA.S        CtlDone
  179.  
  180.  
  181. ****************************** FONT METRICS *******************************
  182.  
  183. DrawWindow
  184.     MOVE.W        #SrcCopy,-(SP)                    ; source mode
  185.     _TextMode
  186.     MOVE.W        #Monaco,-(SP)                    ; Monaco
  187.     _TextFont
  188.     MOVE.W        #9,-(SP)                        ; 9 point
  189.     _TextSize
  190.     MOVE.W        #1,-(SP)                        ; bold
  191.     _TextFace
  192.  
  193. ********************** WRITE APPLICATION HEAP FREEMEM *********************
  194.  
  195.     MOVE.W        #6,-(SP)
  196.     MOVE.W        #10,-(SP)
  197.     _MoveTo
  198.     PEA         #'AppHeap: '
  199.     _DrawString
  200.     _FreeMem                                    ; free memory -> D0
  201.     JSR         PrintNum                        ; draw our free mem
  202.  
  203. ************************* WRITE SYSTEM HEAP FREEMEM ***********************
  204.  
  205.     PEA         #'  SysHeap: '
  206.     _DrawString
  207.     _FreeMem SYS                                ; free memory -> D0
  208.     JSR         PrintNum                        ; draw our free sys mem
  209.  
  210. ***************************** WRITELN VOL INFO ****************************
  211.  
  212.     PEA         #'  Disk: '
  213.     _DrawString
  214.  
  215.     MOVE.L        #HVolumeParam.sizeof,D0         ; size of HFS ParamBlock
  216.     _NewPtr CLEAR                                ; NewPtr -> A0
  217.     BNE.S        Exit                            ; IF Error THEN Exit
  218.     MOVE.L        A0,aPBPtr                        ; save PBPtr in D7
  219.     MOVE.L        DCtlEntry.DCtlWindow(A4),A1        ; get window rec pointer
  220.     LEA         aString(A1),A1                    ; address of string buffer
  221.     MOVE.L        A1,IOParam.ioNamePtr(A0)        ; ioNamePtr = Volume Name
  222.     _PBHGetVInfoSync                            ; _GetVolInfo    info -> A0^
  223.  
  224.     MOVE.L        aPBPtr,A0
  225.     MOVE.L        HVolumeParam.ioVAlBlkSiz(A0),D1    ; block size in D1
  226.     MOVE.W        HVolumeParam.ioVFrBlk(A0),D2     ; free blocks in D2
  227.     MOVE.W        D1,D0                            ; 32 bit * 16 bit multiply
  228.     MULU.W        D2,D0                            ; right half of size
  229.     SWAP        D1
  230.     MOVE.W        D1,D3
  231.     MULU.W        D2,D3                            ; left half of size
  232.     SWAP        D3
  233.     ADD.L        D3,D0                            ; total bytes free on vol
  234.     JSR         PrintNum                        ; write # bytes free
  235.  
  236.     PEA         #' free on '
  237.     _DrawString
  238.     MOVE.W        #4,-(SP)                        ; underlined
  239.     _TextFace
  240.     MOVE.L        aPBPtr,A0
  241.     MOVE.L        HVolumeParam.ioNamePtr(A0),-(SP)    ; offset for volName
  242.     _DrawString
  243.  
  244.     MOVE.L        aPBPtr,A0                        ; free the memory
  245.     _DisposePtr
  246.         
  247. Exit    
  248.     RTS
  249.  
  250. ***************************** SUBROUTINES ****************************
  251.  
  252. PrintNum
  253.  
  254.     ; Binary integer to be drawn at CurPenPos in D0 on entry
  255.     ; number drawn in plain text, bolding restored afterwords
  256.     
  257.     MOVE.L        D0,D6                            ; for safe keeping
  258.     CLR.W        -(SP)                            ; plain text
  259.     _TextFace
  260.     MOVE.L        D6,D0                            ; and back again
  261.     MOVE.L        DCtlEntry.DCtlWindow(A4),A0        ; get window rec pointer
  262.     LEA         aNumStr(A0),A0                    ; get buffer address
  263.     _NumToString                                ; Binary-Decimal Package
  264.     MOVE.L        A0,-(SP)                        ; push the pointer to the str
  265.     _DrawString
  266.     MOVE.W        #1,-(SP)                        ; bold text restored
  267.     _TextFace
  268.     RTS
  269.     
  270. ******************************* DATA AREA **********************************
  271.  
  272. theWindow    DC.W    322,10,338,500            ; window top,left,bottom,right
  273.  
  274.     ENDWITH
  275.     END
  276.  
  277.